//
//  KYPersonInfoView.m
//  kuaiyi
//
//  Created by 杨亮 on 16/3/18.
//  Copyright © 2016年 kuaiyi. All rights reserved.
//

#import "KYPersonInfoView.h"
#import "KYProvinceCityModel.h"

@interface KYPersonInfoView ()<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *diseaseLabel;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *patentNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *contactTextField;
@property (weak, nonatomic) IBOutlet UITextField *relationTextField;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UITextField *provinceTextField;
@property (weak, nonatomic) IBOutlet UITextField *cityTextField;
@property (weak, nonatomic) IBOutlet UITextField *regionTextField;
@property (weak, nonatomic) IBOutlet UIButton *oneBtn;
@property (weak, nonatomic) IBOutlet UIButton *twoBtn;
@property (weak, nonatomic) IBOutlet UIButton *threeBtn;
@property (weak, nonatomic) IBOutlet UIButton *fourBtn;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet UIButton *nextBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topContrast;

@property (nonatomic, strong)NSArray *modelList;
@property (nonatomic, copy)NSString *selectRowName;
@property (nonatomic, assign)NSInteger selectRow;

@end
@implementation KYPersonInfoView
-(NSArray *)modelList{
    if (!_modelList) {
        _modelList = [KYProvinceCityModel getProvinceCityModel];
    }
    return _modelList;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle]loadNibNamed:@"KYPersonInfoView" owner:nil options:nil]lastObject];
        [self setUpUI];
    }
    return self;
}
-(void)setUpUI{
    self.twoBtn.selected = YES;
    self.fourBtn.selected = YES;
    self.nextBtn.layer.cornerRadius = 10;
    self.nextBtn.layer.masksToBounds = YES;
    
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 0, 44)];
    UIBarButtonItem *last = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"illness_button_input_blue.png"] style:UIBarButtonItemStylePlain target:self action:@selector(lastBtnKlick)];
    UIBarButtonItem *next = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"illness_button_input_blue.png"] style:UIBarButtonItemStylePlain target:self action:@selector(nextBtnKlick)];
    UIBarButtonItem *flexible = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *finish = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(finishBtnKlick)];
    toolBar.items = @[last,next,flexible,finish];
    
    for (UIView *views in self.subviews) {
        if ([views isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)views;
            textField.inputAccessoryView = toolBar;
        }
    }

    
    UIPickerView *pickView = [[UIPickerView alloc]init];
    pickView.delegate = self;
    pickView.dataSource = self;
    self.provinceTextField.inputView = pickView;
    self.cityTextField.inputView = pickView;
    self.regionTextField.inputView = pickView;
    self.relationTextField.inputView = pickView;
}
//代理方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if ([self.relationTextField isFirstResponder]) {
        return 3;
    }else if ([self.provinceTextField isFirstResponder]) {
        return self.modelList.count;
    }else if([self.cityTextField isFirstResponder]){
        KYProvinceCityModel *model = self.modelList[self.selectRow];
        return model.cities.count;
    }else if([self.regionTextField isFirstResponder]){
        return 5;
    }else {
        return 0;
    }
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if ([self.relationTextField isFirstResponder]) {
        return @"关系";
    }else if ([self.provinceTextField isFirstResponder]) {
        return [self.modelList[row] name];
    }else if([self.cityTextField isFirstResponder]){
        KYProvinceCityModel *model = self.modelList[self.selectRow];
        return model.cities[row];
    }else if([self.regionTextField isFirstResponder]){
        return @"乡下";
    }else {
        return 0;
    }
    return [self.modelList[row] name];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    self.selectRow = row;
    self.selectRowName = [self.modelList[row]name];
}
-(void)lastBtnKlick{
    NSLog(@"1");
}
-(void)nextBtnKlick{
    NSLog(@"2");
    for (UIView *views in self.subviews) {
        if ([views isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)views;
            if ([textField isFirstResponder]) {
                [textField resignFirstResponder];
//                NSLog(@"textField=%@",textField);
//                [[textField nextResponder]becomeFirstResponder];
//                NSLog(@"nextResponder=%@",[textField nextResponder]);
            }
        }
    }
}
-(void)finishBtnKlick{
//    NSLog(@"3");
    for (UIView *views in self.subviews) {
        if ([views isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)views;
            if ([textField isFirstResponder]) {
                textField.text = self.selectRowName;
            }
        }
    }
//    self.provinceTextField.text = self.selectRow;
    [self endEditing:YES];
}

- (IBAction)btnKlick:(UIButton *)sender {
    if (sender.selected) {
        return;
    }
    switch (sender.tag) {
        case 10:
            self.oneBtn.selected = YES;
            self.twoBtn.selected = NO;
            self.tipLabel.hidden = YES;
            self.topContrast.constant = 0;
            break;
        case 11:
            self.twoBtn.selected = YES;
            self.oneBtn.selected = NO;
            self.tipLabel.hidden = NO;
            self.topContrast.constant = 60;
            break;
        case 12:
            self.threeBtn.selected = YES;
            self.fourBtn.selected = NO;
            break;
        case 13:
            self.fourBtn.selected = YES;
            self.threeBtn.selected = NO;
            break;
            
        default:
            break;
    }
}


@end
